import { asyncRouterMap, constantRouterMap } from '@/router'

const permissionRouter = {
	state:{
		routers: constantRouterMap,
		addRouters: []
	},
	mutations: {
		SET_ROUTERS: (state, routers) => {
			state.addRouters = routers
			state.routers = constantRouterMap.concat(routers)
		}
	},
	actions: {
		setRouters({commit},per) {
			var userRouter = asyncRouterMap.filter(router => {
				var fa = false;
				per.forEach(function(v,i){
					if(Object.keys(v).indexOf(router.name) >= 0){
						fa = true;
						router.children.forEach(function(subV,subI){
							if(Object.values(v)[0].indexOf(subV.name) < 0){
								router.children.splice(subI,1)
							}
						})
					}
				})

				if(fa){
					return true;
				} else {
					return false;
				}
			})

			commit('SET_ROUTERS',userRouter);
		}
	}
}

export default permissionRouter